package com.cjd.common.okhttp3

import com.cjd.common.utils.LogUtils
import com.google.gson.Gson
import com.google.gson.TypeAdapter
import okhttp3.ResponseBody
import retrofit2.Converter

/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/12/2
 * description 自定义retrofit 结果转换器
 */
class GsonResponseConverter<T>(val gson: Gson, val adapter: TypeAdapter<T>) :
    Converter<ResponseBody, T> {

    override fun convert(value: ResponseBody): T? {
        try {
            val originJson = value.string()
            LogUtils.json(originJson)
            return adapter.fromJson(originJson)
        } finally {
            value.close()
        }
    }
}